#!/bin/bash

if [ $# -lt 2 ]; then
	echo "help: ./build_server.sh version log distribution"
	echo "eg: ./build_server.sh 2 "0a12a07d1f98" server"
	echo "eg: ./build_server.sh 2 "0a12a07d1f98" desktop"
	echo "eg: ./build_server.sh 2 "0a12a07d1f98""
	exit 1
fi

# install depandency package
all_package=("build-essential" "bison" "flex" "libncurses5-dev" "libssl-dev" "libidn11-dev" "libidn11")

for package in ${all_package[*]}; do
	if [ -z "`dpkg -l | grep $package`" ]; then
		apt-get install $package -y
	fi
done

if [ -d ./linux-4.19.90 ]; then
	echo "Find ./linux-4.19.90"
else
	echo "Can't find ./linux-4.19.90"
	exit 1
fi

# prepare deb env
if [ $3 = "desktop" ]; then
	distribution=desktop
elif [ $3 = "server" ]; then
	distribution=server
else
	distribution=desktop
fi

# clean Source code.
cd ./linux-4.19.90/
#sudo make distclean
cd ../

# Generate a simple changelog template
touch -f .temp.log
cat debian/changelog > .temp.log
cat <<EOF > debian/changelog
linux-4.19.90 (4.19.90-$1) debian; urgency=low

  * Commit ID $2.

 -- wangming <wangming01@loongson.cn>  $(date -R)
EOF
cat .temp.log >> debian/changelog
rm -rf .temp.log

cat <<EOF > debian/rules
#!/usr/bin/make -f

build:
	\$(MAKE) KERNELRELEASE=4.19.90 ARCH=mips \
	KBUILD_BUILD_VERSION=$1 KBUILD_SRC=

binary-arch:
	\$(MAKE) KERNELRELEASE=4.19.90 ARCH=mips \
	KBUILD_BUILD_VERSION=$1 KBUILD_SRC= intdeb-pkg

clean:
	rm -rf debian/*tmp debian/files
	\$(MAKE) clean

binary: binary-arch
EOF


cat <<EOF > debian/control
Source: linux-4.19.90
Section: kernel
Priority: optional
Maintainer: loongson <loongson@loongson-PC>
Build-Depends: bc, kmod, cpio
Homepage: http://www.kernel.org/

Package: linux-image-$distribution-4.19.90
Architecture: mips64el
Description: Linux kernel, version 4.19.90
 This package contains the Linux kernel, modules and corresponding other
 files, version: 4.19.90.

Package: linux-headers-$distribution-4.19.90
Architecture: mips64el
Description: Linux kernel headers for 4.19.90 on mips64el
 This package provides kernel header files for 4.19.90 on mips64el.
 This is useful for people who need to build external modules

Package: linux-libc-dev
Section: devel
Provides: linux-kernel-headers
Architecture: mips64el
Description: Linux support headers for userspace development
 This package provides userspaces headers from the Linux kernel.  These headers
 are used by the installed headers for GNU glibc and other system libraries.

Package: linux-image-$distribution-4.19.90-dbg
Section: debug
Architecture: mips64el
Description: Linux kernel debugging symbols for 4.19.90
 This package will come in handy if you need to debug the kernel. It provides
 all the necessary debug symbols for the kernel and its modules.
EOF

version=$[$1 - 1]
echo "[INFO]: version - 1 = $version"
echo $version >./linux-4.19.90/.version
cp ./scripts/builddeb ./linux-4.19.90/scripts/package/builddeb
cp -rvf ./debian/ ./linux-4.19.90/

# change dir
cd ./linux-4.19.90/

# load defconfig
if [ "$distribution" = "server" ]; then
	make loongson3_server_defconfig
elif [ "$distribution" = "desktop" ]; then
	make loongson3_defconfig
else
	# Default using loongson3_defconfig
	make loongson3_defconfig
fi

# run main make todo: get current cpu cores as the -j argument.
make -j8

# run main buid command
make intdeb-pkg DISTRIBUTION=$distribution

exit 0
